home *** CD-ROM | disk | FTP | other *** search
/ Computer Select (Limited Edition) / Computer Select.iso / dobbs / v16n07 / masm.asc < prev    next >
Encoding:
Text File  |  1991-06-11  |  3.8 KB  |  185 lines

  1. _MASM'S CHANGING FACE_
  2. by Mike Schmit
  3.  
  4.  
  5.  
  6. [LISTING ONE]
  7.  
  8.   .MODEL small
  9.   .STACK 100                ; reserves 100 bytes for the stack
  10.   .CODE                     ; start of code segment
  11.   main PROC
  12.    .STARTUP                 ; generates startup code
  13.    mov  bx, 1               ; stdout
  14.    mov  cx, msg_len
  15.    mov  dx, offset DGROUP:msg
  16.    mov  ah, 40h             ; write to handle
  17.    int  21h                 ; call DOS to write msg
  18.    .EXIT                    ; generates exit code
  19.   main ENDP
  20.   .DATA                     ; start of data segment
  21.   msg BYTE 'Hello world.'
  22.   msg_len equ $ - msg
  23.   END main                  ; end, specify starting address
  24.  
  25.  
  26.  
  27.  
  28.  
  29. [LISTING TWO]
  30.  
  31.  
  32.  EXTRN GetDC     : far
  33.  EXTRN MoveTo    : far
  34.  EXTRN LineTo    : far
  35.  EXTRN ReleaseDC : far
  36.  
  37.  point_list struc
  38.   x1 dw ?
  39.   y1 dw ?
  40.   x2 dw ?
  41.   y2 dw ?
  42.  point_list ends
  43.         .
  44.         .  (assume bx = hWnd)
  45.         .
  46.         push    bx
  47.         call    GetDC       ; returns hDC
  48.         mov     di, ax
  49.  
  50.         push    di
  51.         push    [si].x1
  52.         push    [si].y1
  53.         call    MoveTo
  54.  
  55.         push    di
  56.         push    [si].x2
  57.         push    [si].y2
  58.         call    LineTo
  59.  
  60.         push    bx
  61.         push    di
  62.         call    ReleaseDC
  63.         .
  64.         .
  65.         .
  66.  
  67.  
  68.  
  69. [LISTING THREE]
  70.  
  71.  GetDC     PROTO FAR PASCAL hWnd:WORD
  72.  MoveTo    PROTO FAR PASCAL hDC:WORD, nX:WORD, nY:WORD
  73.  LineTo    PROTO FAR PASCAL hDC:WORD, nX:WORD, nY:WORD
  74.  ReleaseDC PROTO FAR PASCAL hWnd:WORD, hDC:WORD
  75.  
  76.  option oldstructs
  77.  point_list struct
  78.   x1 word ?
  79.   y1 word ?
  80.   x2 word ?
  81.   y2 word ?
  82.  point_list ends
  83.         .
  84.         .    (assume bx = hWnd)
  85.         .
  86.         invoke  GetDC, bx              ; returns hDC
  87.         mov     di, ax
  88.         invoke  MoveTo, di, [si].x1, [si].y1
  89.         invoke  LineTo, di, [si].x2, [si].y2
  90.         invoke  ReleaseDC, bx, di
  91.         .
  92.         .
  93.         .
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. [LISTING FOUR]
  101.  
  102.  factorial MACRO num
  103.   LOCAL result, factor
  104.   IF num LE 0
  105.    %error factorial parameter out of bounds
  106.   ENDIF
  107.   result = 1
  108.   factor = num
  109.   WHILE factor GT 0
  110.     result = result * factor
  111.     factor = factor - 1
  112.   ENDM
  113.   EXITM %result
  114.  ENDM
  115.  i = 1
  116.  REPEAT 20             ; repeat block macro
  117.    DWORD factorial(i)  ; to generate a table of
  118.    i = i + 1           ; the first 20 factorials
  119.  ENDM
  120.  DWORD factorial(-33)  ; error
  121.  
  122.  
  123.  
  124.  
  125. Example 1. Macro parameters can either be required as designated 
  126. by the REQ keyword or specify a default value
  127.  
  128.  set_cursor_pos MACRO row:REQ, col:REQ, page:=<0>
  129.   mov  dh, row
  130.   mov  dl, col
  131.   mov  bh, page
  132.   int  10h
  133.  ENDM
  134.  ...
  135.  set_cursor_pos 5, 10, 1    ; all parameters supplied
  136.  ...
  137.  set_cursor_pos 7, 15       ; page parameter takes default value
  138.  ...
  139.  set_cursor_pos             ; ERROR: required parameters missing
  140.  
  141.  
  142.  
  143.  
  144. Figure 1: MASM 6.0 contains decision and loop directives (in this 
  145. case, an .IF/.ELSE loop) that are translated to their 
  146. corresponding instructions at assembly time.
  147.  
  148.  .IF  ax <  mem_word1
  149.       mov   mem_word2, 2
  150.  .ELSE
  151.       mov   mem_word2, 3
  152.  .ENDIF
  153.  
  154. The above code is translated to the following:
  155.  
  156.         cmp    ax, mem_word1
  157.         jnb    @C0001
  158.         mov    mem_word2, 2
  159.         jmp    @C0003
  160.  @C0001:
  161.         mov    mem_word2, 3
  162.  @C0003:
  163.  
  164.  
  165.  
  166.  
  167. Figure 2. MASM 6.0 automatically generates a jump fixup when 
  168. there is a jump out of range. Notice this example that the 
  169. generated code is five bytes long instead of two. 
  170.  
  171.  
  172.       cmp ax, error_code
  173.       je  exit_error
  174.       db 128 dup(90h)  ; (128 bytes of code, NOP's here)
  175.  exit_error:
  176.  
  177.  
  178. MASM 6.0 translates this to the following:
  179.  
  180.  
  181.       cmp ax, error_code
  182.       jne $+3          ; Note: $+3 is a relative
  183.                        ; jump 3 bytes ahead
  184.       jmp exit_error
  185.       db 128 dup(90h)
  186.  exit_error:
  187.  
  188.  
  189.  
  190.